package com.bootdo.mall_part.service.impl;

import com.alibaba.dubbo.common.threadpool.support.limited.LimitedThreadPool;
import com.bootdo.mall.dao.MallOrderDao;
import com.bootdo.mall.dao.MallOrderLogDao;
import com.bootdo.mall.domain.MallOrder;
import com.bootdo.mall.domain.MallOrderLog;
import com.bootdo.member.domain.ShequMember;
import com.gargoylesoftware.htmlunit.javascript.host.media.RemotePlayback;
import org.apache.zookeeper.data.Stat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.bootdo.mall_part.dao.MallEvaluateDao;
import com.bootdo.mall_part.domain.MallEvaluate;
import com.bootdo.mall_part.service.MallEvaluateService;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.text.html.HTMLDocument;


@Service
public class MallEvaluateServiceImpl implements MallEvaluateService {
	@Autowired
	private MallEvaluateDao mallEvaluateDao;

	@Autowired
	private MallOrderDao mallOrderDao;

	@Autowired
	private MallOrderLogDao mallOrderLogDao;
	
	@Override
	public MallEvaluate get(Long id){
		return mallEvaluateDao.get(id);
	}
	
	@Override
	public List<MallEvaluate> list(Map<String, Object> map){
		return mallEvaluateDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return mallEvaluateDao.count(map);
	}
	
	@Override
	public int save(MallEvaluate mallEvaluate){
	
		if(mallEvaluate.getId() != null) {
			return mallEvaluateDao.update(mallEvaluate);
		}else {
			return mallEvaluateDao.save(mallEvaluate);
		}		
	
		
	}
	
	@Override
	public int update(MallEvaluate mallEvaluate){
		return mallEvaluateDao.update(mallEvaluate);
	}
	
	@Override
	public int remove(Long id){
		return mallEvaluateDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return mallEvaluateDao.batchRemove(ids);
	}

	@Override
	public List<MallEvaluate> getByGoodsId(Map<String, Object> param) {
		return mallEvaluateDao.getByGoodsId(param);
	}

	@Override
	public Integer getByGoodsIdCount(Map<String, Object> map) {
		return mallEvaluateDao.getByGoodsIdCount(map);
	}

	@Override
	public List<MallEvaluate> getUnreadFather(Set<Long> list, Integer offset, Integer limit) {
		return mallEvaluateDao.getUnreadFather(list, offset, limit);
	}

	@Override
	public int getUnreadFatherCount(Set<Long> list) {
		return mallEvaluateDao.getUnreadFatherCount(list);
	}

	@Override
	public List<MallEvaluate> getByFatherId(Long id) {
		return mallEvaluateDao.getByFatherId(id);
	}

	@Override
	public List<MallEvaluate> getSonList(Map<String, Object> param) {
		return mallEvaluateDao.getSonList(param);
	}

	@Override
	public int setReadedDao(Long grandFatherId, Long endId, Long replierId) {
		return 0;
	}

	@Override
	public List<MallEvaluate> getUnreadAll(Long memberId) {
		return mallEvaluateDao.getUnreadAll(memberId);
	}

	@Transactional
	@Override
	public int setReaded(Long grandFatherId, Long endId, Long replierId) {
		return mallEvaluateDao.setReadedDao(grandFatherId, endId, replierId);
	}

	@Override
	public Map<String, Object> getMemberUnreadEvaluate(ShequMember member, Map<String, Object> param) {

		Map<String, Object> result = new HashMap<>();
		List<MallEvaluate> unreadAll = mallEvaluateDao.getUnreadAll(member.getId());

		Set<Long> idList = new HashSet<>();
		for (MallEvaluate one: unreadAll) {
			if (one.getParentId() == 0) {
				idList.add(one.getId());
			} else {
				idList.add(one.getGrandFatherId());
			}
		}

		Integer pageSize = (Integer) param.get("pageSize");
		Integer pageNo = (Integer) param.get("pageNo");
		if (pageNo != null && pageSize != null) {
			List<MallEvaluate> unreadFather = mallEvaluateDao.getUnreadFather(idList, pageNo * pageSize, pageSize);
			result.put("l", unreadFather);
			int unreadFatherCount = mallEvaluateDao.getUnreadFatherCount(idList);
			result.put("total", unreadFatherCount);
		}

		result.put("total", unreadAll.size());
		return result;
	}

	@Transactional
	@Override
	public int addEvaluates(ShequMember shequMember, MallOrder mallOrder, String content, int starNumber) throws RuntimeException {
		int result = 0;

		Map<String, Object> param = new HashMap<>();
		param.put("orderId", mallOrder.getOrderId());
		List<MallOrderLog> list = mallOrderLogDao.list(param);
//		Long merchantId = mallOrder.getUserId();
		for (MallOrderLog one: list) {

			result = addOneEvaluate(shequMember, mallOrder, one, content, starNumber);
			if (result == 0) {
				throw new RuntimeException("更新评价表失败");
			}

		}

		mallOrder.setCommentFlag(2);
		result = mallOrderDao.update(mallOrder);

		return result;
	}

	private int addOneEvaluate(ShequMember member,MallOrder mallOrder,  MallOrderLog orderLog, String content, int starNumber) {
		MallEvaluate mallEvaluate = new MallEvaluate();

		// 设定内容和星星数
		mallEvaluate.setStarNumber(starNumber);
		mallEvaluate.setContent(content);
		mallEvaluate.setOrderNo(mallOrder.getOrderNo());
		mallEvaluate.setOrderId(mallOrder.getOrderId());
		mallEvaluate.setMemberId(mallOrder.getMemberId());
		mallEvaluate.setMemberNickName(mallOrder.getMemberName());


		// 设定评论者信息
		mallEvaluate.setCommentId(member.getId());
		mallEvaluate.setCommentAvater(member.getAvatar());
		mallEvaluate.setCommentName(member.getRealName());
		mallEvaluate.setCommentType(1);

		mallEvaluate.setMerchantId(mallOrder.getUserId());
		mallEvaluate.setMerchantName(mallOrder.getUserName());
		mallEvaluate.setMemberMobile(mallOrder.getMemberMobile());
		mallEvaluate.setGoodsId(orderLog.getGoodId());
		mallEvaluate.setParentId(0L);

		mallEvaluate.setReplierId(mallOrder.getUserId());
		mallEvaluate.setReplierName(mallOrder.getUserName());
		mallEvaluate.setReplierType(2);

		mallEvaluate.setGrandFatherId(0L);

		return mallEvaluateDao.save(mallEvaluate);
	}
}
